<?php
include_once("model/category.class.php");
include_once("model/user.class.php");
include_once("model/page.class.php");
include_once("model/product.class.php");
include_once("controller/JKUpload.class.php");
include_once("includes/util.php");

/**
 * manage class. Process for all function in manage tab of admin page
 */
class Bproduct 
{
	var $db;
 	var $smarty;
 	
	function Bproduct($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	function addNewProduct(){
		if(isset($_POST['total_photos']))
			$total_photos	= $_POST['total_photos'];
		if(isset($_POST['cat_parent']))
			$cat_parent	= $_POST['cat_parent'];
		if(isset($_POST['product_status']))
			$product_status	= $_POST['product_status'];	
		if(isset($_POST['product_name']))
			$product_name	= $_POST['product_name'];	
		if(isset($_POST['product_thumbnail']) && $_POST['product_thumbnail']!="")
			$product_thumbnail 	 = $_POST['product_thumbnail'];
		if(isset($_POST['price']))
			$price = $_POST['price'];
		if(isset($_POST['short_info']))
			$short_info = $_POST['short_info'];	
		if(isset($_POST['description']))
			$description = $_POST['description'];	
		if(isset($_POST['active']))
			$active = 1;	
			
		$msg = product::validateProduct();	
		
		$arr = array("code" => -1, "message" => _("Could not add product."));
		if($msg == ""){
			if($_FILES['product_thumbnail']['name'] != NULL && $_FILES['product_thumbnail']['name'] != "") {
				$upload = new JKUpload();
				$image = $_FILES['product_thumbnail']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				$namefile = explode(".".$image_type,$_FILES['product_thumbnail']['name']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_product_thumbnail($_FILES['product_thumbnail']['tmp_name'], $namefile['0'], $image_type);
				} 
			}
			if($_FILES['product_name_img']['name'] != NULL && $_FILES['product_name_img']['name'] != "") {
				$upload = new JKUpload();
				$image = $_FILES['product_name_img']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				$namefile = explode(".".$image_type,$_FILES['product_name_img']['name']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$productnameimg = $upload->upload_logo($_FILES['product_name_img']['tmp_name'], $namefile['0'], $image_type);
				} 
			}
			$product = array (
					'cat_parent' => $cat_parent,
					'product_name'=>$product_name,
					'product_image'=>$imagename,
					'product_name_img'=>$productnameimg,
					'product_status' => $product_status,
					'price' => $price,
					'short_info' => $short_info,
					'active' => $active,
					'description' => $description
				);	
			$id = product::saveProduct($this->db, $product);
			if($id >=0 ){
				if($total_photos>0){
					for($i=1;$i<=$total_photos;$i++){
						if($_FILES['product_image_'.$i]['name'] != NULL && $_FILES['product_image_'.$i]['name'] != "") {
							$upload = new JKUpload();
							$image = $_FILES['product_image_'.$i]['name'];
							$path = pathinfo($image);
							$image_type = strtolower($path['extension']);
							$namefile = explode(".".$image_type,$_FILES['product_image_'.$i]['name']);
							if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
								$imagename = $upload->upload_product_thumbnail($_FILES['product_image_'.$i]['tmp_name'], $namefile['0'], $image_type);
							} 
							$photoid = product::saveProductPhotos($this->db, $id,$imagename);
						}	
						
					}
				}
				setcookie("message", $msg,time()+5);
				header("Location: ./?mod=manage&sub1=product") ;
			}else{
				setcookie("message", $msg,time()+5);
				header("Location: ./?mod=manage&sub1=product&act=newproduct") ;	
			}
		}
	}
	
	function editProduct(){
		if(isset($_POST['total_photos']))
			$total_photos	= $_POST['total_photos'];
		if(isset($_POST['cat_parent']))
			$cat_parent	= $_POST['cat_parent'];
		if(isset($_POST['product_status']))
			$product_status	= $_POST['product_status'];	
		if(isset($_POST['product_name']))
			$product_name	= $_POST['product_name'];	
		if(isset($_POST['product_thumbnail']) && $_POST['product_thumbnail']!="")
			$product_thumbnail 	 = $_POST['product_thumbnail'];
		if(isset($_POST['price']))
			$price = $_POST['price'];
		if(isset($_POST['short_info']))
			$short_info = $_POST['short_info'];	
		if(isset($_POST['description']))
			$description = $_POST['description'];	
		if(isset($_POST['active']))
			$active = 1;
		if(isset( $_POST['id'])){
			$id = $_POST['id'];		
			$msgId = 2;
			$msg = product::validateProduct();
			if($msg == ""){
				$product = product::getProduct($this->db, $id);
				if($_FILES['product_thumbnail']['name'] != NULL && $_FILES['product_thumbnail']['name'] != "") {
					$upload = new JKUpload();
					$image = $_FILES['product_thumbnail']['name'];
					$path = pathinfo($image);
					$image_type = strtolower($path['extension']);
					$namefile = explode(".".$image_type,$_FILES['product_image_'.$i]['name']);
					if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
						$imagename = $upload->upload_product_thumbnail($_FILES['product_thumbnail']['tmp_name'], $namefile['0'], $image_type);
					} 
					@unlink("../".UPLOAD_PRODUCT.$product_one->product_thumbnail);
					@unlink("../".UPLOAD_PRODUCT_THUMB.$product_one->product_thumbnail);
				}else{
					$imagename = $product_one->product_thumbnail;
				}
				
				if($_FILES['product_name_img']['name'] != NULL && $_FILES['product_name_img']['name'] != "") {
					$upload = new JKUpload();
					$image = $_FILES['product_name_img']['name'];
					$path = pathinfo($image);
					$image_type = strtolower($path['extension']);
					$namefile = explode(".".$image_type,$_FILES['product_image_'.$i]['name']);
					if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
						$productnameimg = $upload->upload_logo($_FILES['product_name_img']['tmp_name'], $namefile['0'], $image_type);
					} 
					@unlink("../".UPLOAD_PRODUCT.$product_one->product_name_img);
					@unlink("../".UPLOAD_PRODUCT_THUMB.$product_one->product_name_img);
				}else{
					$productnameimg = $product_one->product_thumbnail;
				}
				
				$product = array (
					'id' => $id,
					'cat_parent' => $cat_parent,
					'product_name'=>$product_name,
					'product_image'=>$imagename,
					'product_name_img'=>$productnameimg,
					'product_status' => $product_status,
					'price' => $price,
					'short_info' => $short_info,
					'active' => $active,
					'description' => $description
				);	
				$res = product::updateProduct($this->db, $product);
				if($id > 0) {
					if($total_photos>0){
						for($i=1;$i<=$total_photos;$i++){
							if($_FILES['product_image_'.$i]['name'] != NULL && $_FILES['product_image_'.$i]['name'] != "") {
								$upload = new JKUpload();
								$image = $_FILES['product_image_'.$i]['name'];
								$path = pathinfo($image);
								$image_type = strtolower($path['extension']);
								$namefile = explode(".".$image_type,$_FILES['product_image_'.$i]['name']);
								if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
									$imagename = $upload->upload_product_thumbnail($_FILES['product_image_'.$i]['tmp_name'], $namefile['0'], $image_type);
								} 
								$photoid = product::saveProductPhotos($this->db, $id,$imagename);
							}	
							
						}
					}
					$message = "Product has been updated";
					setcookie("message", $message,time()+5);
					header("Location: ./".$_POST['url']);
				} else {
					$cat['message'] = _('Could not insert new category to database.');
				}
			}else{
				setcookie("message", $msg,time()+5);
				///header("Location: ./?mod=manage&sub1=product&act=editproduct") ;	
			}
				
		}
					
	}
	
	function deleteProduct(){
			$arr = array("code" => -1, "message" => _("Invalid Arguments."));
				
			if(isset( $_POST['id']))
			{
				
				$product = product::getProduct($this->db, $_POST['id']);
				$photos = product::PhotosByProductId($this->db,$_POST['id']);
		
				//// DELETE PHOTOS FOR SILDE PRODUCT
				//////////////////////////////////////
				if($photos!=''){
					foreach($photos as $item){
						@unlink("../".UPLOAD_PRODUCT.$item->product_photo);	
						@unlink("../".UPLOAD_PRODUCT_THUMB.$item->product_photo);	
					}
					product::deletePhotoByProductID($this->db,$_POST['id']);	
				}
					if($_POST['product_image'] != '')
					{
						@unlink(UPLOAD_DIR . $product->product_image);
						@unlink(UPLOAD_DIR ."thumb/". $product->product_image);
					}
				$res = product::deleteProduct($this->db, $_POST['id']);
				
				if($res)
				{
					$arr['code'] = 0;
					$arr['message'] = sprintf(_("Product with Id %s has been deleted."),
											"<b>". $_POST['id'] . "</b>");
					
				}
			}
			
			echo array2json($arr);
			
			return;
	}
	
	/*public function Delete_Product(){
		$arr = array('code' => 0, 'message' => "");
		if(isset($_POST['id']) && $_POST['id']!="")
				$id = $_POST['id'];
		
		$product = BWebshopProductBean::getProduct($this->db,$id);
		$path 	= $product->product_thumbnail;	
		$res = BWebshopProduct::remove_product_photo($id,$path);
		$arr = array('message' => "", 
				'code' => -1);
			
		if($res == true) {
			$arr['code'] = 0;
			$arr['message'] = sprintf(_("Product <u>%s</u> has been deleted."), $id);
		} else {
			$arr['code'] = -1;
			$arr['message'] = sprintf(_("Could not delete Product List <u>%s</u>."), $id);
		}
		echo array2json($arr);		 
	}*/
	
	public function Delete_Photo(){
		$arr = array('code' => 0, 'message' => "");
		if(isset($_POST['id']) && $_POST['id']!="")
				$id = $_POST['id'];
		if(isset($_POST['name']) && $_POST['name']!="")
				$path = $_POST['name'];
		$res = product::deletePhotoProduct($this->db,$id);
		@unlink("../".UPLOAD_PRODUCT.$path);
		@unlink("../".UPLOAD_PRODUCT_THUMB.$path);
		$arr = array('message' => "", 
				'code' => -1);
			
		if($res == true) {
			$arr['code'] = 0;
			$arr['message'] = sprintf(_("Photo <u>%s</u> has been deleted."), $id);
		} else {
			$arr['code'] = -1;
			$arr['message'] = sprintf(_("Could not delete Photo <u>%s</u>."), $id);
		}
		echo array2json($arr);		 
	}
	
	public function remove_product_photo($id,$path){
		@unlink("../".UPLOAD_PRODUCT.$path);
		@unlink("../".UPLOAD_PRODUCT_THUMB.$path);
		$res = BWebshopProductBean::deleteProduct($this->db, $id);
		return $res;
	}
	
}
?>